//
//  UserInfoViewModel.m
//  CityFire
//
//  Created by liuyangzhu on 2017/8/2.
//  Copyright © 2017年 刘养柱. All rights reserved.
//

#import "UserInfoViewModel.h"
#import "NSUserDefaults+Tools.h"
#import "IPToolManager.h"

@implementation UserInfoViewModel

- (void)userLogin:(NSDictionary *)parameter
{
    [YQNetworking requestWithUrl:kLOGIN_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:parameter progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
        
    } successBlock:^(id response, NSError *responseInfo) {
        NSError *error;
        UserInfoModel *userModel = [MTLJSONAdapter modelOfClass:[UserInfoModel class] fromJSONDictionary:response error:&error];
        
        self.responseInfo = error ? error : responseInfo;
        self.userInfoModel = userModel;
        
        [NSUserDefaults writeUserDataWithKey:userModel.xDay ? : @"1" forKey:kNSUSERDEFAULT_KEY_FAVRITORDAYS];
        [NSUserDefaults writeUserDataWithKey:userModel.remind forKey:kNSUSERDEFAULT_KEY_ISMINKD];
        [NSUserDefaults writeUserDataWithKey:userModel.sound forKey:kNSUSERDEFAULT_KEY_ISSOUND];
        [NSUserDefaults writeUserDataWithKey:userModel.vibrate forKey:kNSUSERDEFAULT_KEY_VIBRATE];
        
        
        if (responseInfo.code == 0) {
            [SVProgressHUD showSuccessWithStatus:@"登录成功"];
        }else {
            [SVProgressHUD showErrorWithStatus:responseInfo.domain];
        }
        if (userModel) {
            [NSUserDefaults writeUserDataWithKey:userModel.seeeionID forKey:kNSUSERDEFAULT_KEY_SESSIONID];
            [NSUserDefaults writeUserDataWithKey:userModel.userID forKey:@"userId"];
            [NSUserDefaults writeUserDataWithKey:userModel.userName forKey:@"userName"];
            [NSUserDefaults writeUserDataWithKey:[NSKeyedArchiver archivedDataWithRootObject:userModel] forKey:kNSUSERDEFAULT_KEY_USERINFO];
        }
        [[NSNotificationCenter defaultCenter] postNotificationName:kNOTIFICATION_UPDATEUSERCENTER object:userModel];
        
    } failBlock:^(NSError *error) {
        self.requestError = error;
    }];
}

- (void)userLogOut:(NSDictionary *)parameter
{
    UserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_USERINFO]];
    if (model) {
        [YQNetworking requestWithUrl:kLOGOUT_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:@{@"sessionid" :model.seeeionID, @"username" : model.userName} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            
        } successBlock:^(id response, NSError *responseInfo) {
            
            self.responseInfo = responseInfo;
            
            if (responseInfo.code == 0) {
                [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_SESSIONID];
                [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_USERINFO];
                [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_FAVRITORDAYS];
                [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_ISMINKD];
                [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_ISSOUND];
                [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_VIBRATE];
                
                
                [[NSNotificationCenter defaultCenter] postNotificationName:kNOTIFICATION_LOGOUT object:nil];
                
                [SVProgressHUD showSuccessWithStatus:@"退出成功"];
            }else {
                [SVProgressHUD showErrorWithStatus:responseInfo.domain];
            }
            
        } failBlock:^(NSError *error) {
            self.requestError = error;
            [SVProgressHUD showErrorWithStatus:error.domain];
        }];
    }
    
}


- (void)getUserInfo:(NSDictionary *)parameter
{
    UserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_USERINFO]];
    if (model) {
        [YQNetworking requestWithUrl:kSESSION_INFO_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:@{@"sessionid" :model.seeeionID, @"username" : model.userName, @"ip" : [[IPToolManager sharedManager] currentIpAddress]} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            
        } successBlock:^(id response, NSError *responseInfo) {
            
            NSError *error;
            UserInfoModel *userModel = [MTLJSONAdapter modelOfClass:[UserInfoModel class] fromJSONDictionary:response error:&error];
            
            self.responseInfo = error ? error : responseInfo;
            
            self.userInfoModel = userModel;
            
            if (userModel) {
                [NSUserDefaults writeUserDataWithKey:userModel.seeeionID forKey:kNSUSERDEFAULT_KEY_SESSIONID];
                [NSUserDefaults writeUserDataWithKey:[NSKeyedArchiver archivedDataWithRootObject:userModel] forKey:kNSUSERDEFAULT_KEY_USERINFO];
                [[NSNotificationCenter defaultCenter] postNotificationName:kNOTIFICATION_UPDATEUSERCENTER object:userModel];
            }
            
            if (responseInfo.code == 0) {
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_SESSIONID];
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_USERINFO];
                //            [SVProgressHUD showSuccessWithStatus:@"退出成功"];
            }else {
                [SVProgressHUD showErrorWithStatus:responseInfo.domain];
            }
            
        } failBlock:^(NSError *error) {
            self.requestError = error;
            [SVProgressHUD showErrorWithStatus:error.domain];
        }];
    }
    
    
}

- (void)changePassword:(NSDictionary *)parameter
{
    UserInfoModel *model = [NSKeyedUnarchiver unarchiveObjectWithData:[NSUserDefaults readUserDataWithKey:kNSUSERDEFAULT_KEY_USERINFO]];
    if (model) {
        [YQNetworking requestWithUrl:kChangePasswd_URL requestType:YQHttpRequestTypePOST refreshRequest:YES cache:NO params:@{@"sessionid" :model.seeeionID, @"username" : model.userName, @"newpasswd" : parameter[@"newpasswd"], @"oldpwd" : parameter[@"oldpwd"]} progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
            
        } successBlock:^(id response, NSError *responseInfo) {
            
            //        NSError *error;
            //        UserInfoModel *userModel = [MTLJSONAdapter modelOfClass:[UserInfoModel class] fromJSONDictionary:response error:&error];
            
            self.responseInfo = responseInfo;
            
            if (responseInfo.code == 0) {
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_SESSIONID];
                //            [NSUserDefaults removeUserDataWithkey:kNSUSERDEFAULT_KEY_USERINFO];
                [SVProgressHUD showSuccessWithStatus:@"密码修改成功"];
                
                
                [[NSNotificationCenter defaultCenter] postNotificationName:kNOTIFICATION_ALERT_PWS object:nil];
                
                
                [self userLogOut:nil];
            }else {
                [SVProgressHUD showSuccessWithStatus:responseInfo.domain];
            }
            
        } failBlock:^(NSError *error) {
            self.requestError = error;
            [SVProgressHUD showSuccessWithStatus:error.domain];
        }];
    }
    
    
}
//?username=admin&oldpwd=aaaaa&newpasswd=bbbbbbbb&sessionid=1

@end
